﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using RamGecXNAControls;

using Rectangle = Microsoft.Xna.Framework.Rectangle;
using Rect = System.Drawing.Rectangle;
using Image = System.Drawing.Image;
using Point = System.Drawing.Point;
using Brush = System.Drawing.Brush;
using SolidBrush = System.Drawing.SolidBrush;
using Color = System.Drawing.Color;

namespace Eclipse.NET_Theme_Editor
{
    public partial class FrmMain : Form
    {
        // Data Members
        Themes Theme;
        Rect Selected;
        Image SkinImage;

        // Constructors
        public FrmMain()
        {
            InitializeComponent();
        }

        // Load Data
        protected override void OnLoad(EventArgs e)
        {
            Theme = new Themes();
            PropTheme.SelectedObject = Theme;
            PropTheme.CollapseAllGridItems();
            SkinImage = Image.FromFile("Default.png");
            Surface.Size = SkinImage.Size;
        }

        // Choose New Item
        private void PropTheme_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
        {
            if (e.NewSelection.Value != null)
            {
                bool SelectionValid = true;

                try
                {
                    Rectangle Select = (Rectangle)e.NewSelection.Value;
                    Selected = new Rect(Select.X, Select.Y, Select.Width, Select.Height);
                }
                catch
                {
                    SelectionValid = false;
                }

                if (SelectionValid)
                    Surface.Invalidate();
            }
        }

        // Property Changed
        private void PropTheme_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (PropTheme.SelectedGridItem.Parent.Value != null)
            {
                bool SelectionValid = true;

                try
                {
                    Rectangle Select = (Rectangle)PropTheme.SelectedGridItem.Parent.Value;
                    Selected = new Rect(Select.X, Select.Y, Select.Width, Select.Height);
                }
                catch
                {
                    SelectionValid = false;
                }

                if (SelectionValid)
                    Surface.Invalidate();
            }
        }

        // Draw The Graphics
        private void Surface_Paint(object sender, PaintEventArgs e)
        {
            if (SkinImage != null)
                e.Graphics.DrawImage(SkinImage, new Rect(0, 0, Surface.Width, Surface.Height));

            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.Green)), Selected);
        }

        // Update Status Strip Location Label
        private void Surface_MouseMove(object sender, MouseEventArgs e)
        {
            LblPosition.Text = e.X.ToString() + ", " + e.Y.ToString() + "px";
        }

        // Open Menu
        private void MnuOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog DLG = new OpenFileDialog();
            DLG.Filter = "Skinned Theme Files (*.stf)|*.stf";
            DLG.Title = "Open a Skinned Theme";

            if (DLG.ShowDialog() == DialogResult.OK)
            {
                Theme = new Themes();
                Themes.Load(DLG.FileName, ref Theme);
                PropTheme.SelectedObject = null;
                PropTheme.SelectedObject = Theme;
            }

            DLG.Dispose();
        }

        // Save Menu
        private void MnuSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog DLG = new SaveFileDialog();
            DLG.Filter = "Skinned Theme Files (*.stf)|*.stf";
            DLG.Title = "Save a Skinned Theme";

            if (DLG.ShowDialog() == DialogResult.OK)
            {
                Theme.Save(DLG.FileName);
            }

            DLG.Dispose();
        }

        // Import Texture
        private void MnuImportTexture_Click(object sender, EventArgs e)
        {
            OpenFileDialog DLG = new OpenFileDialog();
            DLG.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            DLG.Title = "Import Theme Texture";

            if (DLG.ShowDialog() == DialogResult.OK)
            {
                SkinImage = Image.FromFile(DLG.FileName);
                Surface.Size = SkinImage.Size;
                Surface.Invalidate();
            }

            DLG.Dispose();
        }

        // Toolstrip Functions
        private void ToolOpen_Click(object sender, EventArgs e)
        {
            MnuOpen_Click(sender, e);
        }
        private void ToolSave_Click(object sender, EventArgs e)
        {
            MnuSave_Click(sender, e);
        }
        private void ToolImportTexture_Click(object sender, EventArgs e)
        {
            MnuImportTexture_Click(sender, e);
        }
    }
}
